/*
 * Date: Oct 9, 2003
 * Time: 7:34:57 PM
 */
package tripleo.histore;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

import javax.swing.*;

import org.apache.commons.codec.binary.Hex;

import tripleo.histore.j2.HiStore_J2;
import tripleo.util.*;

class ViewerFrame2 {

	public static void main(String[] args) {
		String s = args.length > 0 ? args[0] : "backing";
		HiStore_J2 his;
		try {
			his = HiStore_J2.New(s);
			new ViewerFrame2(his);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private int curUrl;

	HiStore_J2 myStore;

	JTextArea ta;

	Object tao;

	JTextField tf, ef;

	Object tfo;

	public ViewerFrame2(HiStore_J2 aStore) {
		myStore = aStore;
		initComponents();
//		setURL("00000000");
		curUrl = 0;
		setUtl(curUrl); // JavaBean property notification
	}

	protected int bcu(int i) {
		if (i < 0)
			return 0;
		//
//		final int max = 0xfffff +0xffff0000;
		final int max = 1000000;
		//
		if (i > max)
			return max;
		return i;
	}

	private void initComponents() {
		final JFrame f = new JFrame();
		final Box b = Box.createVerticalBox();
		tf = new JTextField();
		ef = new JTextField();
		ta = new JTextArea();
		//
		tf.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				tf_actionPerformed(e, tf, tfo);
			}
		});
		//
		JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		final Box pp = Box.createVerticalBox();
		final Box p1 = Box.createHorizontalBox();
		pp.add(tf);
		p1.add(ef);
		p1.add(m1("prev", new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				setUtl((curUrl-1));

			}
		}));
		p1.add(m1("next", new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				setUtl((curUrl+1));

			}

		}));
		p1.add(m1("all", new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub

			}
		}));
		pp.add(p1);

		sp.setLeftComponent(pp);
		sp.setRightComponent(new JScrollPane(ta));
		b.add(sp);
		//
		f.getContentPane().add(b);
		f.setSize(500, 600);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);
	}

	private JButton m1(String string, ActionListener actionListener) {
		final JButton button = new JButton(string);
		button.addActionListener(actionListener);
		return button;
	}

	public void setURL(String aURL) {
		tf.setText(aURL);
		tao = urlToContents(aURL);
		ta.setText((String) tao);
	}

	private void setUtl(int curUrl2) {
		curUrl2=bcu(curUrl2);
		//
		System.err.print(curUrl);
		System.err.println(" "+curUrl2);
		//
		final String mmm = //_hexify(curUrl2);
		UT.hexify(curUrl2);
		//tf.setText(mmm);
		curUrl=curUrl2;
		setURL(mmm);
	}

	private String _hexify(int curUrl2) {
		byte nn[] = new byte[4];
		nn[0] = (byte) (curUrl2 & 0xff000000);
		nn[1] = (byte) (curUrl2 & 0xff0000);
		nn[2] = (byte) (curUrl2 & 0xff00);
		nn[3] = (byte) (curUrl2 & 0xff);
		final String mmm = new String(Hex.encodeHex(nn));
		return mmm;
	}

	public void showError(String anErrorString) {
		ef.setText(anErrorString);
	}

	public void tf_actionPerformed(ActionEvent e, JComponent o, Object m) {
		String s = ((JTextField) o).getText();
		if (s.charAt(0) == '#') {
			try {
				reset(s);
			} catch (InitializationFailure e1) {
				defaultReportError(e1);
			}
		} else {
			try {
				setUtl(Integer.parseInt(s));
			} catch (RuntimeException e1) {
				defaultReportError(e1);
			}
		}
	}

	private void defaultReportError(Exception exception) {
		StringWriter sw;
		PrintWriter sss = new PrintWriter(sw = new StringWriter());
		exception.printStackTrace(sss);
		ta.setText("" + sw.getBuffer());
	}

	private void reset(String s) throws InitializationFailure {
		myStore = HiStore_J2.New(s.substring(1));
		setUtl(0);
	}

	public String urlToContents(String aURL) {
		String R;
		final File file = new File(myStore.backing(), aURL+"-C");
		if (!file.exists()) {
			// Assert.not_reached();
			// handle 404
			R = "404 404 404 404 404 404 404 404 404 404 404 404 ";
			showError("404");
		} else {
			byte[] bbuf;
			final int L = Math.min(new Long(file.length()).intValue(),
					0x0fffffff);
			bbuf = new byte[L];
			InputStream fr = null;
			try {
				fr = new FileInputStream(file);
				fr.read(bbuf);
			} catch (FileNotFoundException e) {
				Assert.not_reached();
			} catch (IOException e) {
				Assert.not_reached();
			}
			R = new String(bbuf);
		}
		return R;
	}
}
